from django import template
from django.conf import settings


register = template.Library()


@register.filter(name='filter')
def change_textarea_to_return(source_string):
    res = source_string.replace('\r\n', "<br>")
    return res


@register.filter(name='qiniu')
def make_qiniu_url(source_string):
    res = 'http://{0}{1}'.format(settings.QINIU_BUCKET_DOMAIN, source_string)
    return res


@register.filter(name='short')
def short_name(name):
    name = name.replace('main/od/', '')
    name = name.replace('main/inout/', '')
    return name


@register.filter(name='mobile')
def mobile_hide(mobile='15068826001'):
    if mobile:
        length = len(mobile)
        if length >= 8:
            mobile_1 = mobile[:length - 7]
            mobile_2 = mobile[-3:]
            mobile = mobile_1 + '****' + mobile_2
    return mobile
